package com.fone.player.play;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.fone.player.ApplicationManage;
import com.fone.player.R;
import com.fone.player.client.AironeRst;
import com.fone.player.client.Callback;
import com.fone.player.client.Error;
import com.fone.player.client.Request;
import com.fone.player.play.adapter.AirOneListAdapter;
import com.fone.player.util.ScreenUtil;

/**
 * 设备选择弹出浮动窗口，点击外部区域可消失
 * 
 * @author kongxiaojun
 * @since 2014-5-15
 */
public class AirOneSelectPopupWindow implements OnCheckedChangeListener{
	 private static final String TAG = "AirOneSelectPopupWindow";
	// 声明PopupWindow对象的引用
	private PopupWindow mPopupWindow;
	private ListView mListView;
	private TextView aironeCodeTextView;
	private CheckBox pcCheckBox;
	private String aircode;
	
	public AirOneSelectPopupWindow(Context context,String aironeCode) {
		this.aircode = aironeCode;
		LayoutInflater LayoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// 获取自定义布局文件的视图
		View popupWindowView = LayoutInflater.inflate(
				R.layout.airone_select_dialog, null);
		mPopupWindow = new PopupWindow(popupWindowView,
				ScreenUtil.dp2px(468), ScreenUtil.dp2px(268));
		mPopupWindow.setBackgroundDrawable(new BitmapDrawable());

		DisplayMetrics dm = context.getResources().getDisplayMetrics();		
		mPopupWindow.setWidth((int)(446*dm.density));
		mPopupWindow.setHeight((int)(256*dm.density));
		
		// 设置焦点
		mPopupWindow.setFocusable(true);
		// 设置点击其他地方 就消失
		mPopupWindow.setOutsideTouchable(true);
		
		pcCheckBox = (CheckBox) popupWindowView.findViewById(R.id.airone_dialog_pc_check);
		pcCheckBox.setOnCheckedChangeListener(this);
		
		
		mListView = (ListView)popupWindowView.findViewById(R.id.airone_dialog_list);
		
		AirOneListAdapter adapter = new AirOneListAdapter();
		mListView.setAdapter(adapter);
		mListView.setItemsCanFocus(false);            
		mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);   
		
		aironeCodeTextView = (TextView)popupWindowView.findViewById(R.id.airone_dialog_air_code);
		aironeCodeTextView.setText(aironeCode);
	}
	
	public void setAricode(String aircode){
		this.aircode = aircode;
		aironeCodeTextView.setText(aircode);
	}

	/**
	 * 创建PopupWindow
	 */
	public void show(View view,boolean isOnlineMedia) {
		if (mPopupWindow != null) {
			if(isOnlineMedia) {
				mPopupWindow.getContentView().findViewById(R.id.airone_dialog_pc_title).setVisibility(View.VISIBLE);
				mPopupWindow.getContentView().findViewById(R.id.airone_dialog_pc).setVisibility(View.VISIBLE);
			} else {
				mPopupWindow.getContentView().findViewById(R.id.airone_dialog_pc_title).setVisibility(View.GONE);
				mPopupWindow.getContentView().findViewById(R.id.airone_dialog_pc).setVisibility(View.GONE);
			}
//            pcCheckBox.setChecked(false);
			mPopupWindow.showAtLocation(view, Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL, 0, 0);	
		}
	}

	public void dismiss() {
		if (mPopupWindow != null && mPopupWindow.isShowing()) {
			mPopupWindow.getContentView().startAnimation(AnimationUtils.loadAnimation(ApplicationManage.getAplicationManageInstance(), R.anim.scale_zoom));
			mPopupWindow.dismiss();
		}
	}

	public View getContentView() {
		if (mPopupWindow != null) {
			return mPopupWindow.getContentView();
		}
		return null;
	}

	public PopupWindow getPopupWindow() {
		return mPopupWindow;
	}
	
	public ListView getListView() {
		return mListView;
	}

	public void showHelpInfo() {
		mListView.setVisibility(View.GONE);
	}
	
	public void showDeviceInfo() {
		mListView.setVisibility(View.VISIBLE);
	}

	public boolean isShowing(){
		return mPopupWindow.isShowing();
	}
	
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		switch (buttonView.getId()) {
		case R.id.airone_dialog_pc_check:
			//pc投放
			if(isChecked){
				//投放
				Request.getInstance().airone(aircode,new Callback<AironeRst>() {
					
					@Override
					public void onSuccess(AironeRst t) {
						
					}
					
					@Override
					public void onFailure(Error error) {
						
					}
				});
				
			}else {
				//没有关闭功能
			}
			break;

		default:
			break;
		}
	}
}
